package org.linlinjava.litemall.db.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.JdbcType;
import org.linlinjava.litemall.db.dto.AdminShopSpecialDateDTO;
import org.linlinjava.litemall.db.handler.AdminShopSpecialDateListHandler;
import org.linlinjava.litemall.db.handler.ArrayObjectJsonTypeHandler;
import org.linlinjava.litemall.db.handler.IntegerArrayJsonTypeHandler;
import org.linlinjava.litemall.db.handler.LocalTimeArrayTypeHandler;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * litemall商店
 *
 * @author yliyun
 * @date 2022/11/21
 */
@TableName(value = "litemall_shop",autoResultMap = true)
@Data
public class LitemallShop {
    /**
     * 被删除
     */
    public static final Boolean IS_DELETED = Deleted.IS_DELETED.value();

    /**
     * 不删除
     */
    public static final Boolean NOT_DELETED = Deleted.NOT_DELETED.value();

    /**
     * id
     */
    @TableId
    private Integer id;

    /**
     * 名字
     */
    private String name;

    /**
     * 税法
     */
    private String taxCode;

    /**
     * 街道地址
     */
    private String streetAddress;

    /**
     * 移动
     */
    private String mobile;

    /**
     * 经度
     */
    private Double longitude;

    /**
     * 纬度
     */
    private Double latitude;

    /**
     * 状态
     */
    private Short status;

    /**
     * 范围
     */
    @TableField("`range`")
    private Integer range;

    /**
     * 开放时间
     */
    private String openTime;

    /**
     * 关闭时间
     */
    private String closeTime;

    /**
     * 类型
     */
    @TableField(typeHandler = IntegerArrayJsonTypeHandler.class,value = "types")
    private Integer[] types;

    /**
     * 周
     */
    @TableField(typeHandler = IntegerArrayJsonTypeHandler.class,value = "weeks")
    private Integer[] weeks;

    /**
     * 创建用户id
     */
    private Integer createUserId;

    /**
     * 添加时间
     */
    private LocalDateTime addTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 删除
     */
    @TableLogic
    private Boolean deleted;

    /**
     * 邮政编码
     */
    private String postalCode;

    /**
     * 恰当单位
     */
    private String aptUnit;

    /**
     * erp代码
     */
    private String erpCode;

    /**
     * 描述
     */
    private String description;
    /**
     * 时区
     */
    private String timezone;

    private String timeDifference;

    @TableField(jdbcType = JdbcType.VARCHAR,typeHandler = LocalTimeArrayTypeHandler.class)
    private List<LocalTime[]> weeksTime;

    @TableField(jdbcType = JdbcType.VARCHAR,typeHandler = AdminShopSpecialDateListHandler.class)
    private List<AdminShopSpecialDateDTO> special;

    private String color;

    public enum Deleted {
        NOT_DELETED(new Boolean("0"), "未删除"),
        IS_DELETED(new Boolean("1"), "已删除");

        private final Boolean value;

        private final String name;

        Deleted(Boolean value, String name) {
            this.value = value;
            this.name = name;
        }

        public Boolean getValue() {
            return this.value;
        }

        public Boolean value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }
    }

    public enum Column {
        id("id", "id", "INTEGER", false),
        name("name", "name", "VARCHAR", true),
        taxCode("tax_code", "taxCode", "VARCHAR", false),
        streetAddress("street_address", "streetAddress", "VARCHAR", false),
        mobile("mobile", "mobile", "VARCHAR", false),
        longitude("longitude", "longitude", "DOUBLE", false),
        latitude("latitude", "latitude", "DOUBLE", false),
        status("status", "status", "SMALLINT", true),
        range("range", "range", "INTEGER", true),
        openTime("open_time", "openTime", "VARCHAR", false),
        closeTime("close_time", "closeTime", "VARCHAR", false),
        types("types", "types", "VARCHAR", false),
        weeks("weeks", "weeks", "VARCHAR", false),
        createUserId("create_user_id", "createUserId", "INTEGER", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false),
        postalCode("postal_code", "postalCode", "VARCHAR", false),
        aptUnit("apt_unit", "aptUnit", "VARCHAR", false),
        erpCode("erp_code", "erpCode", "VARCHAR", false),
        description("description", "description", "LONGVARCHAR", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
